package logic

import (
	"autotestservice/testservices/internal/data"
	"autotestservice/testservices/internal/deviceMng"
	"autotestservice/testservices/internal/svc"
	"autotestservice/testservices/internal/types"
	"context"
	"gitee.com/jmdvirus/jkgo/jk/jklog"
	"github.com/tal-tech/go-zero/core/logx"
)

type AutotestStopLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAutotestStopLogic(ctx context.Context, svcCtx *svc.ServiceContext) AutotestStopLogic {
	return AutotestStopLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AutotestStopLogic) AutotestStop(req types.RequestTestProjectStop) (*types.Response, error) {
	jklog.L().Debugln("test stop ", req.Id, ", ", req.Model)
	res := &types.Response{
		Code:    0,
		Message: "",
	}
	devOpt := deviceMng.NewDeviceOption(req.TestTarget.ConnectStyle, req.TestTarget.Addr, req.TestTarget.Port)
	findDev := deviceMng.GlobalDeviceMng().Find(*devOpt)
	if findDev != nil {
		// 已经连接
		tp := types.TestProject{}
		tp.Id = req.Id
		tp.Model = req.Model
		tp.DeviceId = req.DeviceId
		tp.TestUser = req.TestUser
		_, err := data.AutoTestTestProjectStop(tp)
		if err != nil {
			jklog.L().Errorln(err)
			res.Code = 10
			res.Message = err.Error()
		} else {
			res.Message = "success"
		}
	} else {
		res.Code = 1
		res.Message = "device not connect"
	}

	return res, nil
}
